package cs575.update;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;

import cs575.common.Pair;
import cs575.common.Player.Position;
import cs575.update.services.AddPlayer;
import cs575.update.services.UpdatePlayer;

public class UpdateClient extends JFrame implements ActionListener {
	private static final long serialVersionUID = -5853105834000914584L;

	private JTextField server;
	private JTextField name;
	private JComboBox<Position> position;
	private DefaultListModel<Pair<String, Integer>> sModel;
	private JList<Pair<String, Integer>> statList;

	public UpdateClient() {
		super("UpdateClient");

		SpringLayout layout = new SpringLayout();
		JPanel content = new JPanel(layout);

		JLabel titleLabel = new JLabel("Update Client");
		layout.putConstraint(SpringLayout.NORTH, titleLabel, 10,
				SpringLayout.NORTH, content);
		layout.putConstraint(SpringLayout.WEST, titleLabel, 10,
				SpringLayout.WEST, content);
		content.add(titleLabel);

		server = new JTextField("127.0.0.1:7777");
		layout.putConstraint(SpringLayout.NORTH, server, 10,
				SpringLayout.SOUTH, titleLabel);
		layout.putConstraint(SpringLayout.EAST, server, -10, SpringLayout.EAST,
				content);
		content.add(server);

		JLabel serverLabel = new JLabel("Server:");
		layout.putConstraint(SpringLayout.VERTICAL_CENTER, serverLabel, 0,
				SpringLayout.VERTICAL_CENTER, server);
		layout.putConstraint(SpringLayout.WEST, serverLabel, 10,
				SpringLayout.WEST, content);
		content.add(serverLabel);

		name = new JTextField();
		layout.putConstraint(SpringLayout.NORTH, name, 10, SpringLayout.SOUTH,
				server);
		layout.putConstraint(SpringLayout.EAST, name, -10, SpringLayout.EAST,
				content);
		content.add(name);

		JLabel nameLabel = new JLabel("Name:");
		layout.putConstraint(SpringLayout.VERTICAL_CENTER, nameLabel, 0,
				SpringLayout.VERTICAL_CENTER, name);
		layout.putConstraint(SpringLayout.WEST, nameLabel, 10,
				SpringLayout.WEST, content);
		content.add(nameLabel);

		DefaultComboBoxModel<Position> model = new DefaultComboBoxModel<Position>();
		for (Position p : Position.values()) {
			model.addElement(p);
		}
		position = new JComboBox<Position>(model);
		layout.putConstraint(SpringLayout.NORTH, position, 10,
				SpringLayout.SOUTH, name);
		layout.putConstraint(SpringLayout.EAST, position, -10,
				SpringLayout.EAST, content);
		content.add(position);

		JLabel positionLabel = new JLabel("Position:");
		layout.putConstraint(SpringLayout.VERTICAL_CENTER, positionLabel, 0,
				SpringLayout.VERTICAL_CENTER, position);
		layout.putConstraint(SpringLayout.WEST, positionLabel, 10,
				SpringLayout.WEST, content);
		content.add(positionLabel);

		JButton addStat = new JButton("Add Stat");
		addStat.setActionCommand("ADD_STAT");
		addStat.addActionListener(this);
		layout.putConstraint(SpringLayout.NORTH, addStat, 10,
				SpringLayout.SOUTH, position);
		content.add(addStat);

		JLabel sLabel = new JLabel("Stats:");
		layout.putConstraint(SpringLayout.VERTICAL_CENTER, sLabel, 0,
				SpringLayout.VERTICAL_CENTER, addStat);
		layout.putConstraint(SpringLayout.WEST, sLabel, 10, SpringLayout.WEST,
				content);
		content.add(sLabel);

		JButton removeStat = new JButton("Remove Stat");
		removeStat.setActionCommand("REMOVE_STAT");
		removeStat.addActionListener(this);
		layout.putConstraint(SpringLayout.NORTH, removeStat, 10,
				SpringLayout.SOUTH, position);
		content.add(removeStat);

		sModel = new DefaultListModel<Pair<String, Integer>>();
		statList = new JList<Pair<String, Integer>>(sModel);
		JScrollPane scroll = new JScrollPane(statList);
		layout.putConstraint(SpringLayout.NORTH, scroll, 10,
				SpringLayout.SOUTH, sLabel);
		layout.putConstraint(SpringLayout.WEST, scroll, 10, SpringLayout.WEST,
				content);
		layout.putConstraint(SpringLayout.EAST, scroll, -10, SpringLayout.EAST,
				content);
		SpringLayout.Constraints constr = layout.getConstraints(scroll);
		constr.setHeight(Spring.constant(100));
		content.add(scroll);

		layout.putConstraint(SpringLayout.WEST, server, 10, SpringLayout.EAST,
				positionLabel);
		layout.putConstraint(SpringLayout.WEST, name, 10, SpringLayout.EAST,
				positionLabel);
		layout.putConstraint(SpringLayout.WEST, position, 10,
				SpringLayout.EAST, positionLabel);
		layout.putConstraint(SpringLayout.WEST, addStat, 10, SpringLayout.EAST,
				positionLabel);
		layout.putConstraint(SpringLayout.WEST, removeStat, 10,
				SpringLayout.EAST, addStat);

		JButton add = new JButton("Add");
		add.setActionCommand("ADD");
		add.addActionListener(this);
		layout.putConstraint(SpringLayout.NORTH, add, 10, SpringLayout.SOUTH,
				scroll);
		layout.putConstraint(SpringLayout.WEST, add, 10, SpringLayout.WEST,
				content);
		content.add(add);

		JButton update = new JButton("Update");
		update.setActionCommand("UPDATE");
		update.addActionListener(this);
		layout.putConstraint(SpringLayout.NORTH, update, 10,
				SpringLayout.SOUTH, scroll);
		layout.putConstraint(SpringLayout.WEST, update, 10, SpringLayout.EAST,
				add);
		content.add(update);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setContentPane(content);
		setSize(new Dimension(500, 350));
		setLocationRelativeTo(null);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String command = arg0.getActionCommand();

		if (command.equals("ADD")) {
			String[] split = server.getText().split(":");

			try {
				Integer.parseInt(split[1]);
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(this,
						"Invalid server port number.", "Input Error",
						JOptionPane.ERROR_MESSAGE);
				return;
			}

			if (name.getText().trim().equals("")) {
				JOptionPane.showMessageDialog(this,
						"Enter a non-empty player name.", "Input Error",
						JOptionPane.ERROR_MESSAGE);
				return;
			}

			AddPlayer ap = new AddPlayer();
			ap.execute(split[0], Integer.parseInt(split[1]), name.getText(),
					(Position) position.getSelectedItem());
		} else if (command.equals("ADD_STAT")) {
			StatDialog sd = new StatDialog(this);
			sd.setVisible(true);

			Pair<String, Integer> stat = sd.getStat();

			if (stat != null) {
				sModel.addElement(stat);
			}
		} else if (command.equals("REMOVE_STAT")) {
			if (statList.getSelectedIndex() != -1) {
				sModel.remove(statList.getSelectedIndex());
				statList.repaint();
			}
		} else if (command.equals("UPDATE")) {
			if (name.getText().trim().equals("")) {
				JOptionPane.showMessageDialog(this,
						"Enter a non-empty player name.", "Input Error",
						JOptionPane.ERROR_MESSAGE);
				return;
			}

			String[] split = server.getText().split(":");
			List<Pair<String, Integer>> sList = new Vector<>();
			for (int i = 0; i < sModel.getSize(); i++) {
				sList.add(sModel.getElementAt(i));
			}

			UpdatePlayer up = new UpdatePlayer();
			up.execute(split[0], Integer.parseInt(split[1]), name.getText(),
					sList);

		}

	}

	/**
	 * Client main method.
	 * 
	 * @param args
	 *            - Not used.
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				UpdateClient uc = new UpdateClient();
				uc.setVisible(true);
			}
		});
	}

}
